import datetime
import random
import time
from multiprocessing import Pool
import multiprocessing
import os

"""定义装饰器"""
def pro_zsq(fun):
    def inner(*args,**kwargs):
        print("**********抢票开始**********")
        btime = time.time()
        fun(*args,**kwargs)
        etime = time.time()
        use_time = etime - btime
        print("**********抢票结束**********")
        print(f"累计抢票持续时间:{round(use_time,3)}s")
    return inner

class Worker():
    amount = 100000
    count = 0
    @pro_zsq
    def run(self):
        while True:
            w = random.randint(1, 10)
            if Worker.amount == 0:
                print("票已经抢完了,请下次再来")
                print(f"累计抢票次数:{Worker.count}次")
                break
            if Worker.amount - w < 0:
                continue
            Worker.amount  -= w
            Worker.count += 1
            time_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            print(f"{time_now}已经抢了{w}张,剩余{Worker.amount}张")


if __name__ == "__main__":
    # 产生一个进程池
    pool = Pool(processes=4)  # processes最好和cpu核数相等
    worker = Worker()
    pc = pool.apply_async(worker.run())
    pool.close()
    pool.join()
